import axios from "axios"
export default {
    namespaced: true, // 开启命名空间
    state: {
        sum: 0,
    },
    actions: {
        incrementOdd(context, value) {
            if (context.state.sum % 2) {
                context.commit('increment', value)
            }
        },
        incrementWait(context, value) {
            setTimeout(() => {
                context.commit('increment', value)
            }, 500)
        },
        async incrementRandomServer(context) {
            try {
                const res = await axios.get('/api/random')
                console.log('请求成功', res)
                context.commit('increment', res.data)
            } catch (err) {
                console.log('请求失败', err)
            }
        }
    },
    mutations: {
        increment(state, value) {
            state.sum += value
        },
        decrement(state, value) {
            state.sum -= value
        },
    },
    getters: {
        bigSum(state) {
            return state.sum * 10
        }
    }
}